# -*- coding: utf-8 -*-
"""
@author: haoyanghuang
@brief: utils for distance computation

"""

import sys
import warnings
warnings.filterwarnings("ignore")

try:
    import lzma
    import Levenshtein
except:
    pass
import numpy as np
from difflib import SequenceMatcher
from sklearn.metrics.pairwise import cosine_similarity

sys.path.append("..")

MISSING_VALUE_NUMERIC = -1.



def _cosine_sim(vec1, vec2):
    try:
        s = cosine_similarity(vec1.reshape(1, -1), vec2.reshape(1, -1))[0][0]
    except:
        try:
            s = cosine_similarity(vec1, vec2)[0][0]
        except:
            s = MISSING_VALUE_NUMERIC
    return s



#simple sim calc
def _unitvec(vec, norm='l2'):
    if norm == 'l1':
        veclen = np.sum(np.abs(vec))
    if norm == 'l2':
        veclen = np.sqrt(np.sum(vec** 2))
    if veclen > 0.0:
        return vec / veclen
    else:
        return vec
# faster sim
def _calc_similarity(ws1, ws2):
    if not(len(ws1) and len(ws2)):
        raise ZeroDivisionError('Atleast one of the passed list is empty.')
    n_w1 = _unitvec(np.array(ws1))
    n_w2 = _unitvec(np.array(ws2))
    return np.dot(np.array(n_w1),np.array(n_w2))